package com.one.square.oauth.exception;

import com.one.square.core.domain.Result;
import com.one.square.core.enums.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SerializationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author cwm
 * @Description 全局异常处理
 * @date 2021/10/26 下午1:10
 * @Version 1.0
 */
@RestControllerAdvice
@Slf4j
public class ExceptionAdviceHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result unKnowExceptionHandler(Exception e) {
        log.error(e.getMessage());
        return Result.failed(e.getMessage());
    }

    @ExceptionHandler(value = ForbiddenException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result forbiddenException(ForbiddenException e){
        log.error(e.getMessage());
        return Result.failed(ResultCode.ACCESS_UNAUTHORIZED);
    }

    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result unauthorizedException(UnauthorizedException e){
        log.error(e.getMessage());
        return Result.failed(ResultCode.AUTHORIZED_ERROR);
    }

    @ExceptionHandler(value = MethodNotAllowed.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public Result methodNotAllowed(MethodNotAllowed e){
        log.error(e.getMessage());
        return Result.failed(ResultCode.METHOD_NOT_ALLOWED);
    }

    @ExceptionHandler(value = ServerErrorException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result methodNotAllowed(ServerErrorException e){
        log.error(e.getMessage());
        return Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
    }
}
